package com.intellagric.service.agritech.impl;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.dao.agritech.AgtUserTagDao;
import com.intellagric.mapper.AgtUserTagMapper;
import com.intellagric.module.agritech.AgtUserTagVo;
import com.intellagric.pojo.AgtUserTag;
import com.intellagric.pojo.AgtUserTagExample;
import com.intellagric.service.agritech.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: cwq
 * @Date: 2019/2/26 11:38
 * @Description:
 */
@Service
public class UserTagServiceImpl implements UserTagService {

    @Autowired
    private AgtUserTagMapper agtUserTagMapper;

    @Autowired
    private AgtUserTagDao agtUserTagDao;

    @Override
    public ResponseMessage addUserTag(AgtUserTag userTag) {
        userTag.setId(UUIDUtils.getID());
        userTag.setCreatedTime(new Date());
        return agtUserTagMapper.insertSelective(userTag) == 1 ? ResponseMessage.success().add("add", userTag) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteUserTag(String id) {
        return agtUserTagMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editUserTag(AgtUserTag userTag) {
        return agtUserTagMapper.updateByPrimaryKeySelective(userTag) == 1 ? ResponseMessage.success().add("update", userTag) : ResponseMessage.fail();
    }

    @Override
    public AgtUserTag getUserTagById(String id) {
        return agtUserTagMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getUserTagList() {
        AgtUserTagExample agtUserTagExample = new AgtUserTagExample();
        agtUserTagExample.setOrderByClause("created_time ASC");
        agtUserTagExample.createCriteria().andIdIsNotNull();
        List<AgtUserTag> agtUserTagList = agtUserTagMapper.selectByExample(agtUserTagExample);
        return LayuiDataGridResult.success().add(agtUserTagList, agtUserTagList.size());
    }

    @Override
    public LayuiDataGridResult getUserTagByPage(int page, int limit) {
        PageHelper.startPage(page, limit);
        AgtUserTagExample userTagExample = new AgtUserTagExample();
        userTagExample.createCriteria().andIdIsNotNull();
        List<AgtUserTag> userTagList = agtUserTagMapper.selectByExample(userTagExample);
        return LayuiDataGridResult.success().add(userTagList, this.getUserTagList().getCount());
    }

    @Override
    public LayuiDataGridResult getUserTagListByVo(AgtUserTagVo agtUserTagVo, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<AgtUserTag> userTagList = agtUserTagDao.selectUserTagListByVo(agtUserTagVo);
        return LayuiDataGridResult.success().add(userTagList, userTagList.size());
    }

    @Override
    public List<AgtUserTag> getUserTagSumWeightByVo(AgtUserTagVo agtUserTagVo) {
        AgtUserTagExample userTagExample = new AgtUserTagExample();
        return agtUserTagDao.selectUserTagSumWeightByVo(agtUserTagVo);
    }

    @Override
    public Map<String, Object> getUserTagSumActionCountGroupByActionIdByVo(AgtUserTagVo agtUserTagVo) {

        List<String> legenndDate = new ArrayList<String>(){{
            add("浏览");
            add("搜索");
            add("点赞");
            add("收藏");
            add("评论");
        }};

        List<Map> series = new ArrayList<>();
        for (final String action : legenndDate) {
            series.add(new HashMap<String, Object>() {{
                put("name", action);
                put("type", "bar");
                put("data", new int[7]);
            }});
        }


//        Map<String, Object> map = new HashMap<>();
//        map.put("series", new HashMap<String, Map>());
//        ((Map) map.get("series")).put("浏览", new int[7]);
//        ((Map) map.get("series")).put("搜索", new int[7]);
//        ((Map) map.get("series")).put("点赞", new int[7]);
//        ((Map) map.get("series")).put("收藏", new int[7]);
//        ((Map) map.get("series")).put("评论", new int[7]);

        List<Date> dateList = new ArrayList<>();
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -8); //日减1
        for (int i = 0; i <= 7; i++) {
            ca.add(Calendar.DATE, 1); //日减1
            dateList.add(ca.getTime());

            agtUserTagVo.setCreatedTime(ca.getTime());
            List<AgtUserTag> list = agtUserTagDao.selectUserTagSumActionCountGroupByActionIdByVo(agtUserTagVo);
            for (AgtUserTag item : list) {
                for (Map seriesItem : series) {
                    if (seriesItem.get("name").equals(item.getActionName())) {
                        ((int[])seriesItem.get("data"))[i] = item.getActionCount();
                    }
                }
//                ((int[])((Map) map.get("series")).get(item.getActionName()))[i] = item.getActionCount();
            }
        }

        List<String> xAxisData = new ArrayList<>();
        for (Date date : dateList) {
            xAxisData.add(new SimpleDateFormat("yyyy-MM-dd").format(date));
        }

        Map<String, Object> objMap = new HashMap<>();
        objMap.put("legendData", legenndDate);
        objMap.put("xAxisData", xAxisData);
        objMap.put("series", series);

        return objMap;
//        map.put("dates", dateList);
//        return agtUserTagDao.selectUserTagSumActionCountGroupByActionIdByVo(agtUserTagVo);
    }
}
